package cn.com.dpc.adapter;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import cn.com.dpc.DevListActivity;
import cn.com.dpc.ParamsActivity;
import cn.com.dpc.R;

public class LvButtonAdapter extends BaseAdapter {
	private class buttonViewHolder {
		TextView alarmname;
		TextView alarmtime;
		TextView confirmtime;
		TextView url;
		ImageButton alarmadd;
	}

	private List<HashMap<String, Object>> mAppList;
	private LayoutInflater mInflater;
	private Context mContext;
	private String[] keyString;
	private int[] valueViewID;
	private buttonViewHolder holder;

	public LvButtonAdapter(Context c, List<HashMap<String, Object>> appList,
			int resource, String[] from, int[] to) {
		mAppList = appList;
		mContext = c;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		keyString = new String[from.length];
		valueViewID = new int[to.length];
		System.arraycopy(from, 0, keyString, 0, from.length);
		System.arraycopy(to, 0, valueViewID, 0, to.length);
	}

	@Override
	public int getCount() {
		return mAppList.size();
	}

	@Override
	public Object getItem(int position) {
		return mAppList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void removeItem(int position) {
		mAppList.remove(position);
		this.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView != null) {
			holder = (buttonViewHolder) convertView.getTag();
		} else {
			convertView = mInflater.inflate(R.layout.listconfalarm, null);
			holder = new buttonViewHolder();
			holder.alarmname = (TextView) convertView
					.findViewById(valueViewID[0]);
			holder.alarmtime = (TextView) convertView
					.findViewById(valueViewID[1]);
			holder.confirmtime = (TextView) convertView
					.findViewById(valueViewID[2]);
			holder.url = (TextView) convertView.findViewById(valueViewID[3]);
			holder.alarmadd = (ImageButton) convertView
					.findViewById(valueViewID[4]);
			convertView.setTag(holder);
		}

		HashMap<String, Object> appInfo = mAppList.get(position);
		if (appInfo != null) {
			String alarmname = (String) appInfo.get(keyString[0]);
			String alarmtime = (String) appInfo.get(keyString[1]);
			String confirmtime = (String) appInfo.get(keyString[2]);
			String url = (String) appInfo.get(keyString[3]);
			int alarmadd = (Integer) appInfo.get(keyString[4]);
			holder.alarmname.setText(alarmname);
			holder.alarmtime.setText(alarmtime);
			holder.confirmtime.setText(confirmtime);
			holder.url.setText(url);
			holder.alarmadd.setImageDrawable(holder.alarmadd.getResources()
					.getDrawable(alarmadd));
			holder.alarmadd.setOnClickListener(new LvButtonListener(position));
		}
		return convertView;
	}

	class LvButtonListener implements OnClickListener {
		private int position;

		LvButtonListener(int pos) {
			position = pos;
		}

		@Override
		public void onClick(View v) {
			int vid = v.getId();
			if (vid == holder.alarmadd.getId()) {
				// removeItem(position);

				Intent intent = new Intent(mContext, ParamsActivity.class);
				intent.setFlags(1);
				Bundle bundle = new Bundle();
				bundle.putString("url", 2 + "_" + 11);
				bundle.putString("text", mAppList.get(position).get("name")
						.toString());
				intent.putExtras(bundle);
				mContext.startActivity(intent);
			}
		}
	}
}
